// 验证手机号
function checkMobile (rule, value, callback) {
  const regMobile = /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
  if (regMobile.test(value)) {
    return callback()
  }
  callback(new Error('请输入正确的手机号'))
}

// 防抖函数
function debounce (fn, delay = 300) {
  let timeout = null
  return function (...args) {
    if (timeout) clearTimeout(timeout)
    timeout = setTimeout(() => {
      // console.log(args)
      fn(args)
    }, delay)
    // console.log(timeout)
  }
}

// 节流函数
function throttle (fn, delay = 300) {
  let timeout = null
  return function (...args) {
    if (timeout) return false
    fn.apply(this, args)
    timeout = setTimeout(() => {
      timeout = null
    }, delay)
  }
}

// 双重点击
function doubleClick (fn, delay = 300) {
  let timeout = null
  let isDoubleClick = false
  return function (...args) {
    if (timeout) clearTimeout(timeout)
    timeout = setTimeout(() => {
      isDoubleClick = false
    }, delay)
    if (isDoubleClick) {
      fn.apply(this, ...args)
    }
    isDoubleClick = true
  }
}

// url拼接query
function urlJointQuery (url, query) {
  const queryArr = []
  // 如果query是一个空对象则直接返回url
  if (!Object.keys(query).length) return url
  // 不是空对象则拼接到url后面
  for (const key in query) {
    queryArr.push(`${key}=${query[key]}`)
  }
  return `${url}?${queryArr.join('')}`
}

// 返回基于currentIndex的pathArr的+1或者-1
function handlePath (history, change = 0) {
  if (!history) return false
  return history.pathArr[history.currentIndex + change]
}

// 用setTimeout模拟的setInterval
/**
 * @param { Function } callback
 * @param { Number } delay
 * @param { Array } timerId
 */
function _setInterval (callback, delay = 1000, timerId) {
  // 若timerId本身存在计时器则清空
  while (timerId.length) {
    clearTimeout(timerId[0])
    timerId.shift()
  }
  function fn () {
    callback()
    const prevTimerId = timerId[0]
    clearTimeout(prevTimerId)
    while (timerId.length) {
      timerId.shift()
    }
    timerId.push(setTimeout(fn, delay))
  }
  // 触发
  timerId.push(setTimeout(fn, delay))
}

// 处理换行符
function switchEnter (str) {
  // str.replace('↵', '222')
  str = str.replace(/\n/g, '<br/>')
  return str
}

// 处理歌词
function handleLyric (lyric) {
  // 如果传入参数错误返货false
  if (!lyric) return false
  // 存入一个把检测到语法错误的语句添加到里面的数组
  const toBeDelece = []
  const addTime = []
  // 用换行将他们分开
  const result = lyric.split(/\n/).map((value, index) => {
    // 存放结果的obj 用于return
    const obj = {}
    // 正则检测
    const regResult = value.match(/(\[.+\])(.+)?/)
    // 语法非法, 添加到toBeDelete
    if (!regResult) {
      addTime.push(index)
      // 检查是 有歌词没时间 还是空文本
      if (value) {
        obj.lyric = value.trim()
      // 空文本
      } else {
        obj.lyric = ''
      }
      return obj
    // 只有时间
    } else if (regResult[0] === regResult[1]) {
      toBeDelece.push(index)
      return ''
    }
    // 将测是否属于有时间但是是空文本的结果
    regResult[1] = regResult[1].split(':')
    obj.time = (regResult[1][0].match(/[0-9]+/)[0] * 60 + parseFloat(regResult[1][1])).toFixed(3)
    regResult[2] ? (obj.lyric = regResult[2].trim()) : (obj.lyric = '')
    return obj
  })
  // console.log(result)
  // 为没有时间的加上时间
  addTime.forEach(index => {
    if (index === result.length - 1) {
      result.splice(index, 1)
      // result[index].time = result[index - 1].time + 5
    } else {
      result[index].time = result[index + 1].time - 0.001
      if (result[index].time < 0) result[index].time = 0
    }
  })
  // 倒过来遍历, 逐个删除掉
  toBeDelece.reverse().forEach(index => {
    result.splice(index, 1)
  })
  return result
}
export {
  checkMobile,
  debounce,
  throttle,
  doubleClick,
  urlJointQuery,
  handlePath,
  _setInterval,
  switchEnter,
  handleLyric
}
